﻿#region Apache 2.0 License header

//   Copyright 2012 Lesley van Zijl
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License. 

#endregion

using System;
using System.Reflection;
using FavourObjects;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests
{
    [TestClass]
    public class MethodRegistryTests
    {
        [TestMethod]
        public void GetMethod_HappyFlow_NonNullReturnValue()
        {
            MemberInfoCache methodRegistry = MemberInfoCache.ActiveCache;
            MethodInfo methodInfo = methodRegistry.GetMethod(typeof (Type), "GetMethod", typeof (String));

            Assert.IsNotNull(methodInfo);
        }

        [TestMethod]
        public void TryFindInRegistry_MethodHappyFlow_FoundAndReturned()
        {
            MemberInfoCache methodRegistry = MemberInfoCache.ActiveCache;
            methodRegistry.GetMethod(typeof (Type), "GetMethod", typeof (String));

            var publicMethodRegistry = FavourObject.Create<IPublicMethodRegistry>(methodRegistry);

            var method = new Method
                             {
                                 MemberType = MemberTypes.Method,
                                 Name = "GetMethod",
                                 Owner = typeof (Type),
                                 Parameters = new[] {typeof (String)}
                             };

            MemberInfo result;
            Assert.IsTrue(publicMethodRegistry.TryFindInRegistry(method, out result));
            Assert.IsNotNull(result);
        }

        [TestMethod]
        public void GetProperty_HappyFlow_NonNullReturnValue()
        {
            MemberInfoCache methodRegistry = MemberInfoCache.ActiveCache;
            PropertyInfo methodInfo = methodRegistry.GetProperty(typeof (Type), "IsVisible", typeof (bool));

            Assert.IsNotNull(methodInfo);
        }

        [TestMethod]
        public void TryFindInRegistry_PropertyHappyFlow_FoundAndReturned()
        {
            MemberInfoCache methodRegistry = MemberInfoCache.ActiveCache;
            methodRegistry.GetProperty(typeof (Type), "IsVisible", typeof (bool));

            var publicMethodRegistry = FavourObject.Create<IPublicMethodRegistry>(methodRegistry);

            var method = new Method
                             {
                                 MemberType = MemberTypes.Property,
                                 Name = "IsVisible",
                                 Owner = typeof (Type),
                                 Parameters = new[] {typeof (bool)}
                             };
            MemberInfo result;
            Assert.IsTrue(publicMethodRegistry.TryFindInRegistry(method, out result));
            Assert.IsNotNull(result);
        }

        #region Nested type: IPublicMethodRegistry

        public interface IPublicMethodRegistry
        {
            bool TryFindInRegistry(Method method, out MemberInfo memberInfo);
        }

        #endregion
    }
}